package jgl.vajra.kingkong.widget.manager;

import android.content.Context;
import android.location.Address;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import jgl.vajra.kingkong.bean.AddressBean;
import jgl.vajra.kingkong.bean.CityChoiceBean;
import jgl.vajra.kingkong.bean.PeripheralPoiBean;
import jgl.vajra.kingkong.constant.Constant;
import jgl.vajra.kingkong.manager.AccountManager;
import jgl.vajra.kingkong.widget.bean.JPushBean;
import jgl.vajra.kingkong.widget.bean.PushBean;
import jgl.vajra.kingkong.widget.push.JPushManager;
import jgl.vajra.kingkong.widget.push.PushManager;
import jgl.vajra.kingkong.widget.utils.SharedPrefsUtils;
import jgl.vajra.kingkong.widget.utils.UIUtils;

/**
 * Created by Mloong on 2018/8/2.
 */

public class SPManager {

    public static class LoginData {
        public static boolean getIsLogin() {
            return SharedPrefsUtils.getBooleanPreference("isLogin", false);
        }

        public static void setIsLogin(boolean isLogin) {
            SharedPrefsUtils.setBooleanPreference("isLogin", isLogin);
        }
    }

    public static class UserData {
        public static void saveUserInfo(Context context, String userJsonData) {
            SharedPrefsUtils.setStringPreference(context, "userInfo", userJsonData);
        }

        public static String getUserInfo(Context context) {

            return SharedPrefsUtils.getStringPreference(context, "userInfo");
        }


        public static void saveLoginData(Context context, String loginInfoBeanString) {
            SharedPrefsUtils.setStringPreference(context, Constant.USER_TYPE + "loginData", loginInfoBeanString);
        }

        public static String getLoginData(Context context) {
            if (context == null) context = UIUtils.getContext();
            return SharedPrefsUtils.getStringPreference(context, Constant.USER_TYPE + "loginData");
        }
    }

    public static class PushData {
        public static void savePushData(String pushMsg, String pushType) {
            PushBean pushBean = new PushBean(pushMsg, pushType);
            String pushString = JSON.toJSONString(pushBean);
            SharedPrefsUtils.setStringPreference(AccountManager.getInstance().getUserId() + PushManager.getInstance().PUSH_DATA, pushString);

        }

        public static PushBean getPushData() {
            String pushString = SharedPrefsUtils.getStringPreference(UIUtils.getContext(), AccountManager.getInstance().getUserId() + PushManager.getInstance().PUSH_DATA);
            if (TextUtils.isEmpty(pushString)) return null;
            PushBean pushBean = JSONObject.parseObject(pushString, PushBean.class);
            return pushBean;
        }

        public static void clearPushData() {
            SharedPrefsUtils.setStringPreference(AccountManager.getInstance().getUserId() + PushManager.getInstance().PUSH_DATA, "");
        }
    }

    public static class JPushData {
        public static void saveJPushData(String pushMsgId, String romType, String pushTitle, String pushContent, String pushExtras) {
            JPushBean jPushBean = new JPushBean(pushMsgId, romType, pushTitle, pushContent, pushExtras);
            String pushString = JSON.toJSONString(jPushBean);
            SharedPrefsUtils.setStringPreference(AccountManager.getInstance().getUserId() + JPushManager.getInstance().JPUSH_DATA, pushString);

        }

        public static JPushBean getJPushData() {
            String pushString = SharedPrefsUtils.getStringPreference(UIUtils.getContext(), AccountManager.getInstance().getUserId() + JPushManager.getInstance().JPUSH_DATA);
            if (TextUtils.isEmpty(pushString)) return null;
            JPushBean pushBean = JSONObject.parseObject(pushString, JPushBean.class);
            return pushBean;
        }

        public static void clearJPushData() {
            SharedPrefsUtils.setStringPreference(AccountManager.getInstance().getUserId() + JPushManager.getInstance().JPUSH_DATA, "");
        }

        public static boolean getOpenPushTag() {
            return SharedPrefsUtils.getBooleanPreference(AccountManager.getInstance().getUserId() + "OpenPushTag", true);
        }

        public static void setOpenPushTag(boolean isOpen) {
            SharedPrefsUtils.setBooleanPreference(AccountManager.getInstance().getUserId() + "OpenPushTag", isOpen);
        }
    }

    public static class AgreementSplashData {
        public static void saveAgreementStatus(boolean b) {
            SharedPrefsUtils.setBooleanPreference(Constant.USER_TYPE +"saveAgreementStatus", b);

        }

        public static boolean getAgreementStatus() {
            return SharedPrefsUtils.getBooleanPreference(Constant.USER_TYPE +"saveAgreementStatus", true);

        }
    }

    public static class MineData {
        public static void saveHistoricalRecordsInfo(Context context, List<PeripheralPoiBean> list) {
            String data = new Gson().toJson(list);
            SharedPrefsUtils.setStringPreference(context, Constant.USER_TYPE + "historicalRecords", data);
        }

        public static List<PeripheralPoiBean> getHistoricalRecordsInfo(Context context) {
            String historicalRecords = SharedPrefsUtils.getStringPreference(context, Constant.USER_TYPE + "historicalRecords");
            List<PeripheralPoiBean> list = new ArrayList<>();
            if (!TextUtils.isEmpty(historicalRecords)) {
                list.addAll(new Gson().fromJson(historicalRecords, new TypeToken<List<PeripheralPoiBean>>() {
                }.getType()));
            }
            return list;
        }

        public static void saveVoicePlayback(boolean isVoicePlayback) {
            SharedPrefsUtils.setBooleanPreference(Constant.USER_TYPE +"voicePlayback", isVoicePlayback);

        }

        public static boolean getVoicePlayback() {
            return SharedPrefsUtils.getBooleanPreference(Constant.USER_TYPE +"voicePlayback", true);

        }
    }

    public static class HomeData {

        //保存RAS私有密钥
        public static void saveRASPrivateKey(String rASPrivateKey) {
            SharedPrefsUtils.setStringPreference(Constant.USER_TYPE + "rASPrivateKey", rASPrivateKey);

        }

        public static String getRASPrivateKey() {
            return SharedPrefsUtils.getStringPreference(UIUtils.getContext(), Constant.USER_TYPE + "rASPrivateKey");

        }

        public static void saveLatelyAddressInfo(Context context, List<CityChoiceBean> list) {
            String data = new Gson().toJson(list);
            SharedPrefsUtils.setStringPreference(context, Constant.USER_TYPE + "latelyAddress", data);
        }

        public static List<CityChoiceBean> getLatelyAddressInfo(Context context) {
            String historicalRecords = SharedPrefsUtils.getStringPreference(context, Constant.USER_TYPE + "latelyAddress");
            List<CityChoiceBean> list = new ArrayList<>();
            if (!TextUtils.isEmpty(historicalRecords)) {
                list.addAll(new Gson().fromJson(historicalRecords, new TypeToken<List<CityChoiceBean>>() {
                }.getType()));
            }
            return list;
        }


        public static void saveAddressData(AddressBean address) {
            String addressString = JSON.toJSONString(address);
            SharedPrefsUtils.setStringPreference(Constant.USER_TYPE + "address", addressString);

        }

        public static AddressBean getAddressData() {
            String pushString = SharedPrefsUtils.getStringPreference(UIUtils.getContext(), Constant.USER_TYPE + "address");
            if (TextUtils.isEmpty(pushString)) return null;
            AddressBean addressBean = JSONObject.parseObject(pushString, AddressBean.class);
            return addressBean;
        }

        public static void saveSelectLocationData(CityChoiceBean cityChoice) {
            String cityChoiceString = JSON.toJSONString(cityChoice);
            SharedPrefsUtils.setStringPreference(Constant.USER_TYPE + "cityChoice", cityChoiceString);

        }

        public static CityChoiceBean getSelectLocationData() {
            String pushString = SharedPrefsUtils.getStringPreference(UIUtils.getContext(), Constant.USER_TYPE + "cityChoice");
            if (TextUtils.isEmpty(pushString)) return null;
            CityChoiceBean cityChoiceBean = JSONObject.parseObject(pushString, CityChoiceBean.class);
            return cityChoiceBean;
        }

        //记录socket最后一次心跳包的时间
        public static void saveSocketHeartbeatLastTime(Long time) {
            SharedPrefsUtils.setLongPreference(Constant.USER_TYPE + "time", time);

        }

        public static Long getSocketHeartbeatLastTime() {
            return SharedPrefsUtils.getLongPreference(Constant.USER_TYPE + "time",0);

        }
    }

}
